package com.zhengshang.third.phone;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhengshang.api.domain.AppPhoneArea;
import com.zhengshang.api.service.IAppPhoneAreaService;
import com.zhengshang.common.utils.StringUtils;
import com.zhengshang.common.utils.http.AliHttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class PhoneAreaUtils {

    /**
     * 接口请求地址
     */
    public static final String baseUrl = "https://jmtransfer.market.alicloudapi.com";

    /**
     * 具体接口
     */
    public static final String apiUrl = "/mobile/transfer";

    /**
     * 淘宝应用密钥
     */
    public static final String appCode = "c69f400e244f4e009fe02b8ff8e2a05f";


    @Autowired
    private IAppPhoneAreaService appPhoneAreaService;

    /**
     * 通过接口查询手机号归属地
     *
     * @param mobile
     */
    private String queryPhoneInfo(String mobile) {
        String result = "";
        try {
            String method = "POST";
            Map<String, String> headers = new HashMap<>();
            //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
            headers.put("Authorization", "APPCODE " + appCode);
            //根据API的要求，定义相对应的Content-Type
            headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            Map<String, String> querys = new HashMap<>();
            Map<String, String> bodys = new HashMap<>();
            bodys.put("mobile_number", mobile);
            HttpResponse response = AliHttpUtils.doPost(baseUrl, apiUrl, method, headers, querys, bodys);
            //{"data":{"area":"河南-漯河","originalIsp":"电信","newIsp":"电信","isTransfer":0},"msg":"成功","success":true,"code":200,"taskNo":"55088822493024531053"}
            result = EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取手机号信息,有的话从数据库读
     * @param mobile
     * @return
     */
    public AppPhoneArea getPhoneInfo(String mobile) {
        AppPhoneArea appPhoneArea = appPhoneAreaService.queryByPhone(mobile);
        if (StringUtils.isNull(appPhoneArea)) {
            String result = queryPhoneInfo(mobile);
            if (StringUtils.isNotEmpty(result)) {
                JSONObject jsonObject = JSON.parseObject(result);
                Boolean success = jsonObject.getBoolean("success");
                if (success) {
                    JSONObject data = jsonObject.getJSONObject("data");
                    String area = data.getString("area");
                    String originalIsp = data.getString("originalIsp");
                    String newIsp = data.getString("newIsp");
                    Integer isTransfer = data.getInteger("isTransfer");
                    AppPhoneArea appPhoneArea1 = new AppPhoneArea();
                    appPhoneArea1.setPhone(mobile);
                    appPhoneArea1.setPhoneNewIsp(newIsp);
                    appPhoneArea1.setPhoneOriginalIsp(originalIsp);
                    appPhoneArea1.setPhoneArea(area);
                    appPhoneArea1.setIsTransfer(isTransfer);
                    appPhoneAreaService.insertAppPhoneArea(appPhoneArea1);
                    appPhoneArea = appPhoneArea1;
                }
            }
        }
        return appPhoneArea;
    }

}
